﻿using Basic.FlowApplication.Interface;
using Basic.FlowApplication.Model;
using Basic.FlowCollect;
using Basic.FlowModel.AuditCopyLog;
using Basic.FlowModel.AuditLogItem;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.Application.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.Extend
{
    internal class CopyAuditService : ICopyAuditService
    {
        private readonly IFlowAuditCopyLogCollect _Copy;
        private readonly IAuditLogService _AuditLog;
        private readonly IFlowNodeService _Flow;

        public CopyAuditService ( IFlowAuditCopyLogCollect copy,
            IAuditLogService auditLog,
            IFlowNodeService flow )
        {
            this._Copy = copy;
            this._AuditLog = auditLog;
            this._Flow = flow;
        }

        public CopyNodeLog GetCopyLog ( long copyId, long empId )
        {
            AuditCopy obj = this._Copy.Get<AuditCopy>(copyId);
            if ( obj.ToEmpId != empId )
            {
                throw new ErrorException("flow.emp.no.prower");
            }
            if ( obj.Status != FlowMsgStatus.已发送 )
            {
                throw new ErrorException("flow.copy.status.error");
            }
            AuditItemLog log = this._AuditLog.GetItem(obj.ItemId);
            AuditFlowBody flow = this._Flow.Get(log.FlowId);
            FlowNode node = flow.Nodes[log.NodeId];
            return new CopyNodeLog
            {
                EmpId = log.EmpId,
                DocTitle = log.DocTitle,
                AuditOpinion = log.AuditOpinion,
                AuditType = node.AuditType,
                FormId = node.FormId.GetValueOrDefault(flow.DefFormId.Value),
                Id = log.Id,
                Status = log.AuditState,
                LinkPk = log.LinkPk,
                LinkType = log.LinkType,
                LogId = log.LogId,
                NodeName = node.NodeName,
                NodeType = log.NodeType,
                AuditStatus = log.LogAuditStatus,
            };
        }
    }
}
